package student.action;

import java.util.ArrayList;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.util.MyLog;

import edu.student.models.Lookup;

/**
 * 
 * @author Sai Naga Sravani Peraka
 */
public class DeleteLookup {
	private int lookup_id=0;
	int i=0;
	private String table="";
	private String name = "";
	private String is_current = "";
	private String submit="";
	Lookup lookup =new Lookup();
    private ArrayList<Lookup> allValues = new ArrayList<Lookup>();
    private String msg="";
    private String del_msg="";
    String field="";
	public String execute() throws Exception {
		MyLog.log("In DeleteLookup.execute().......");
		Map session = ActionContext.getContext().getSession();
		session = ActionContext.getContext().getSession();
		table=(String)session.get("table");
		MyLog.log("session table"+session.get("table"));
		System.out.println("here");
        int result = Lookup.findOnefordelete(table,"where "+table+"_id = " + lookup_id);
        System.out.println("result value:"+result);
		if(result==0){
			del_msg="delete_fail";
			lookup=Lookup.findOne(table,"where "+table+"_id = " + lookup_id);
			if(table.equals("interest")){
				field="Announcements";
			}
			else if(table.equals("subject")){
				field="create courses in semester";
			}
			else if(table.equals("faculty")){
				field="courses";
			}
	     	String value=lookup.getName();
			msg="Cannot Delete "+table+" "+value +". Please delete the dependent values with "+value+" in "+field+"  and try again!!";
  			MyLog.log("Cannot Delete, "+value +" is associated with other fields. Please delete the dependent values with "+table+" and try again!!");
  			 return "displayMessage";
		}
		MyLog.log("lookup value deleted successfully");
		MyLog.log(lookup+" deleted successfully...!");
		this.allValues = Lookup.find(table,"");
		return "success";
		}
	public int getLookup_id() {
		return lookup_id;
	}
	public void setLookup_id(int lookup_id) {
		this.lookup_id = lookup_id;
	}
	public String getTable() {
		return table;
	}
	public void setTable(String table) {
		this.table = table;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getIs_current() {
		return is_current;
	}
	public void setIs_current(String is_current) {
		this.is_current = is_current;
	}
	public String getSubmit() {
		return submit;
	}
	public void setSubmit(String submit) {
		this.submit = submit;
	}
	public Lookup getLookup() {
		return lookup;
	}
	public void setLookup(Lookup lookup) {
		this.lookup = lookup;
	}
	public ArrayList<Lookup> getAllValues() {
		return allValues;
	}
	public void setAllValues(ArrayList<Lookup> allValues) {
		this.allValues = allValues;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getDel_msg() {
		return del_msg;
	}
	public void setDel_msg(String del_msg) {
		this.del_msg = del_msg;
	}
}
